package org.footito.android.ui.activity;

import greendroid.app.GDListActivity;

import org.footito.android.R;
import org.footito.android.data.Status;
import org.footito.android.data.database.FootitoDatabaseAdapter;
import org.footito.android.tools.ConvertingTools;
import org.footito.android.tools.Log;
import org.footito.android.ui.adapters.CommentsBaseAdapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class StatusInfoActivity extends GDListActivity {

	private static final String STATUS_ID = "statusid";
	private FootitoDatabaseAdapter dbAdapter;
	private Status status;

	private TextView eventcontentTV;
	private TextView teamName;
	private ImageView teamFlag;
	private TextView timestamp;
	private TextView permanentLink;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getActionBar().setTitle("Status");

//		setContentView(R.layout.activity_event_info);
		View header = LayoutInflater.from(this).inflate(R.layout.activity_event_info, null);
		getListView().addHeaderView(header);

		dbAdapter = FootitoDatabaseAdapter.getInstance(this);
		eventcontentTV = (TextView) header.findViewById(R.id.eventText);
		teamFlag = (ImageView) header.findViewById(R.id.blason);
		teamName = (TextView) header.findViewById(R.id.teamName);
		timestamp = (TextView) header.findViewById(R.id.TV_date);
		permanentLink = (TextView) header.findViewById(R.id.tv_permanentLink);

		permanentLink.setMovementMethod(LinkMovementMethod.getInstance());
	}

	@Override
	protected void onResume() {
		super.onResume();
		

		long statusId = getIntent().getExtras().getLong(STATUS_ID);
		status = dbAdapter.getStatus(statusId);
		Log.d(status.getContent());
		if (status == null)
			finish();
		else {
			teamFlag.setImageBitmap(status.getTeam().getPicture(this));
			teamName.setText(status.getTeam().getName());
			timestamp.setText(ConvertingTools.getFormattedDate(status.getTimestamp()));

			eventcontentTV.setText(status.getContent());

			permanentLink.setText(Html.fromHtml("<a href=\"" + status.getLink() + "\">Lien permanent</a>"));
			// <a href="http://www.footito.fr/event/35130">Lien permanent</a>
			
			setListAdapter(new CommentsBaseAdapter(this, status.getComments()));
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onNewIntent(Intent intent) {
		setIntent(intent);
		super.onNewIntent(intent);
	}

	public static void launchActivity(Context context, Status status) {
		Intent intent = new Intent(context, StatusInfoActivity.class);
		intent.putExtra(STATUS_ID, status.getId());
		context.startActivity(intent);
	}
}
